package com.gitee.swsk33.catcomment.service.impl;

import com.gitee.swsk33.catcomment.dao.CommentDAO;
import com.gitee.swsk33.catcomment.service.CommentService;
import com.gitee.swsk33.catentity.dataobject.Cat;
import com.gitee.swsk33.catentity.dataobject.Comment;
import com.gitee.swsk33.catentity.model.Result;
import com.gitee.swsk33.catfeign.feignclient.CatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class CommentServiceImpl implements CommentService {

	@Autowired
	private CommentDAO commentDAO;

	@Autowired
	private CatClient catClient;

	@Override
	public Result<Void> add(Comment comment) {
		// 省略值校验
		Result<Void> result = new Result<>();
		if (commentDAO.insert(comment) < 1) {
			result.setResultFailed("插入失败！");
			return result;
		}
		result.setResultSuccess("插入成功！", null);
		return result;
	}

	@Override
	public Result<Comment> getById(int id) {
		Result<Comment> result = new Result<>();
		Comment getComment = commentDAO.selectById(id);
		if (getComment == null) {
			result.setResultFailed("评论不存在！");
			return result;
		}
		// 远程调用组装猫对象
		Result<Cat> getCat = catClient.getById(getComment.getCatId());
		if (!getCat.isSuccess()) {
			result.setResultFailed("对应的猫猫不存在！");
			return result;
		}
		getComment.setCat(getCat.getData());
		result.setResultSuccess("查询成功！", getComment);
		return result;
	}

}